#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>

#include "app_init.h"
#include "cmsis_os2.h"
#include "osal_debug.h"
#include "soc_osal.h"
#include "errno.h"
#include "errcode.h"
#include "pinctrl.h"
#include "common_def.h"

#include "gpio.h"
#include "pwm.h"
#include "pwm_porting.h"

#include "feeling.h"
#include "pwm_patch.h"
#include "common_config.h"

volatile int g_lightOn = 0;  // 呼吸灯开关状态(0-4)
volatile int g_currentMode = 0; // 当前模式
volatile int g_breathingColor = 0; // 用于循环呼吸灯的颜色选择(0-6)
static int currentDuty = 2;

void all_channel_stop(void){
    IoTPwmStop(PWM_PORT_NUM_RED);
    IoTPwmStop(PWM_PORT_NUM_GREEN);
    IoTPwmStop(PWM_PORT_NUM_BLUE);
}

void SetLedState(int mode) {
    // 先停止所有PWM
    all_channel_stop();
    
    g_currentMode = mode;
    currentDuty = 1;
    g_breathingColor = 0; // 重置循环呼吸灯颜色选择
}

void PWMBreathingEffect(void) {
    static int direction = 1; // 1表示增加亮度，0表示减小亮度
    if (direction) {
        currentDuty += BREATHING_STEP;
        if (currentDuty >= IOT_PWM_DUTY_MAX) {
            currentDuty = IOT_PWM_DUTY_MAX;
            direction = 0;
        }
    } else {
        currentDuty -= BREATHING_STEP;
        if (currentDuty <= 1) {
            currentDuty = 1;
            direction = 1;
            // 如果是循环呼吸灯模式，切换颜色
            if (g_currentMode == 4) {
                g_breathingColor = (g_breathingColor + 1) % 7;
                all_channel_stop();
            }
        }
    }
    
    // 根据当前模式设置PWM
    switch(g_currentMode) {
        case 1: // 红色呼吸灯
            IoTPwmStart(PWM_PORT_NUM_RED, currentDuty, Frequency);
            break;
        case 2: // 绿色呼吸灯
            IoTPwmStart(PWM_PORT_NUM_BLUE, currentDuty, Frequency);
            break;
        case 3: // 蓝色呼吸灯
            IoTPwmStart(PWM_PORT_NUM_GREEN, currentDuty, Frequency);
            break;
        case 4: // 七色循环呼吸灯
            switch(g_breathingColor) {
                case 0: // 红色
                    IoTPwmStart(PWM_PORT_NUM_RED, currentDuty, Frequency);
                    break;
                case 1: // 绿色
                    IoTPwmStart(PWM_PORT_NUM_GREEN, currentDuty, Frequency);
                    break;
                case 2: // 蓝色
                    IoTPwmStart(PWM_PORT_NUM_BLUE, currentDuty, Frequency);
                    break;
                case 3: // 红+绿
                    IoTPwmStart(PWM_PORT_NUM_RED, currentDuty, Frequency);
                    IoTPwmStart(PWM_PORT_NUM_GREEN, currentDuty, Frequency);
                    break;
                case 4: // 绿+蓝
                    IoTPwmStart(PWM_PORT_NUM_GREEN, currentDuty, Frequency);
                    IoTPwmStart(PWM_PORT_NUM_BLUE, currentDuty, Frequency);
                    break;
                case 5: // 红+蓝
                    IoTPwmStart(PWM_PORT_NUM_RED, currentDuty, Frequency);
                    IoTPwmStart(PWM_PORT_NUM_BLUE, currentDuty, Frequency);
                    break;
                case 6: // 红+绿+蓝
                    IoTPwmStart(PWM_PORT_NUM_RED, currentDuty, Frequency);
                    IoTPwmStart(PWM_PORT_NUM_GREEN, currentDuty, Frequency);
                    IoTPwmStart(PWM_PORT_NUM_BLUE, currentDuty, Frequency);
                    break;
            }
            break;
    }
}

void PWMLedInit(void){
    uapi_pin_set_mode(RED_LED_PIN_NAME, RED_LED_PIN_FUNCTION);
    uapi_pin_set_mode(GREEN_LED_PIN_NAME, GREEN_LED_PIN_FUNCTION);
    uapi_pin_set_mode(BLUE_LED_PIN_NAME, BLUE_LED_PIN_FUNCTION);
    IoTPwmInit(PWM_PORT_NUM_RED);
    IoTPwmInit(PWM_PORT_NUM_GREEN);
    IoTPwmInit(PWM_PORT_NUM_BLUE);
}

void PWMLed(int g_lightOn)
{
    if (g_lightOn != g_currentMode) {
        // 模式改变，立即更新LED状态
        SetLedState(g_lightOn);
    }
    
    if (g_currentMode > 0) {
        // 呼吸灯效果
        PWMBreathingEffect();
        usleep(DELAY_US_2);
    } else {
        // 关闭所有灯
        all_channel_stop();
        usleep(DELAY_US_2);
    }
}
